package com.remenote.remenoteback.service.impl;


import com.remenote.remenoteback.dao.*;
import com.remenote.remenoteback.entity.doo.*;
import com.remenote.remenoteback.entity.resp.ServerResponse;
import com.remenote.remenoteback.entity.vo.*;
import com.remenote.remenoteback.service.StudyGroupService;
import com.remenote.remenoteback.util.ThreadLocalUtil;
import jakarta.transaction.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
class StudyGroupServiceImpl implements StudyGroupService {
    @Autowired
    private StudyGroupRepository studyGroupRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private NoteRepository noteRepository;
    @Autowired
    private CardRepository cardRepository;
    @Autowired
    private DeckRepository deckRepository;

    @Override
    public ServerResponse<List<StudyGroupVo>> findAll() {
        List<StudyGroupDo> studyGroupDos= studyGroupRepository.findAll();
        return ServerResponse.buildSuccessResponse(StudyGroupVo.fromDos(studyGroupDos));
    }

    @Override
    @Transactional
    public ServerResponse<List<NoteVo>> findGroupNote() {
        UserDo userDo=userRepository.findByUsername(ThreadLocalUtil.getUserName());
        List<UserDo> groupMembers= userRepository.findByGroupName(userDo.getGroupName());
        List<NoteVo> noteVos=new ArrayList<>();
        for (UserDo groupMember : groupMembers) {
            List<NoteDo> noteDos=noteRepository.findNoteDoByUsernameAndIsPublicAndIsDelete(groupMember.getUsername(), true, false);
            List<NoteVo> tempNoteVos = new ArrayList<>();
            for (NoteDo noteDo : noteDos) {
                tempNoteVos.add(NoteVo.fromNoteDoWithNickname(noteDo, groupMember.getNickname()));
            }
            noteVos.addAll(tempNoteVos);
        }
        return ServerResponse.buildSuccessResponse(noteVos);
    }

    @Override
    public ServerResponse findStudyGroup(String groupName) {
        StudyGroupDo studyGroupDo = studyGroupRepository.findStudyGroupDoByGroupName(groupName);
        return ServerResponse.buildSuccessResponse(new StudyGroupVo().setStudyGroupName(groupName));
    }

    @Override
    public ServerResponse createStudyGroup(String groupName) {
        StudyGroupDo studyGroupDo = new StudyGroupDo();
        studyGroupDo.setGroupName(groupName);
        studyGroupRepository.save(studyGroupDo);
        return ServerResponse.buildSuccessResponse();
    }

    @Override
    @Transactional
    public ServerResponse searchInventory(String groupName) {
        StudyGroupDo studyGroupDo = studyGroupRepository.findStudyGroupDoByGroupName(groupName);
        if (studyGroupDo == null) {
            throw new RuntimeException("没有这个小组");
        }
        List<UserDo> users = userRepository.findByGroupName(groupName);
        List<NoteVo> noteVos = new ArrayList<>();
        List<DeckVo> deckVos =new ArrayList<>();
        for (UserDo user : users) {
            deckRepository.findDeckDoByUsernameAndIsDelete(user.getUsername(),false)
                    .forEach((deck)->{
                        List<CardDo> cardDoList = cardRepository.findCardDoByUsernameAndDeckId(ThreadLocalUtil.getUserName(), deck.getId());
                        deckVos.add(DeckVo.fromDeckDoAndCardList(deck,cardDoList));
                    });
            noteRepository.findNoteDoByIsPublicAndUsernameAndIsDelete(true, user.getUsername(), false)
                    .forEach((note) -> {
                        noteVos.add(NoteVo.fromNoteDoWithNickname(note, user.getNickname()));
                    });
        }
        return ServerResponse.buildSuccessResponse(new StudyGroupVo()
                .setStudyGroupName(groupName)
                .setDecks(deckVos)
                .setNotes(noteVos));
    }
}